package com.xcity.game.item;

import java.util.Date;

import com.xcity.db.entity.EntityState;
import com.xcity.util.IdUtils;

import naga.x.App;
import naga.x.game.GameUnit;
import naga.x.game.template.TemplateService;

public class GameItem implements Item, GameUnit {
	
	protected EntityState state;
	
	protected Bag bag;
	protected long id; // 物品实例id,全服唯一 @see IdUtils#nextItemId
	protected int count;
	protected Date createTime;
	protected ItemTemplate template;
	
	public GameItem(Bag bag, com.xcity.db.entity.Item entity) {
		this(bag,
				entity.getId(),
				entity.getCount(),
				entity.getCreateTime(),
				App.getApp().getServiceManager().get(TemplateService.class).get(ItemTemplate.class, entity.getItemId()),
				EntityState.UPDATE);
	}
	
	public GameItem(Bag bag, ItemTemplate template) {
		this(bag, template, 1);
	}
	
	public GameItem(Bag bag, ItemTemplate template, int count) {
		this(bag, IdUtils.nextItemId(), count, new Date(), template, EntityState.INSERT);
	}
	
	private GameItem(Bag bag, long id, int count, Date createTime, ItemTemplate template, EntityState state) {
		this.bag = bag;
		this.id = id;
		this.count = count;
		this.createTime = createTime;
		this.template = template;
		this.state = state;
	}

	public Bag getBag() {
		return bag;
	}

	@Override
	public long getId() {
		return id;
	}

	@Override
	public int getCount() {
		return count;
	}

	@Override
	public Date getCreateTime() {
		return createTime;
	}

	@Override
	public ItemTemplate getTemplate() {
		return template;
	}
	
	public EntityState getState() {
		return state;
	}
	
	public void delete() {
		state = EntityState.DELETE;
	}

	@Override
	public void setId(long id) {
		this.id = id;
	}

	@Override
	public String getName() {
		return getTemplate().getName();
	}

	@Override
	public void setName(String name) {
		throw new UnsupportedOperationException();
	}

	@Override
	public String getIcon() {
		return getTemplate().getIcon();
	}

	@Override
	public void setIcon(String icon) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int getLevel() {
		return 0;
	}

	@Override
	public void setLevel(int level) {
		throw new UnsupportedOperationException();
	}

	@Override
	public byte getSex() {
		return 0;
	}

	@Override
	public void setSex(byte sex) {
		throw new UnsupportedOperationException();
	}
	
}
